package com.usian.comment.service.impl;

import com.usian.comment.feign.ApUserClient;
import com.usian.comment.service.CommentService;
import com.usian.model.comment.dto.CommentGetDto;
import com.usian.model.comment.dto.CommentLikeDto;
import com.usian.model.comment.dto.CommentSaveDto;
import com.usian.model.comment.pojos.ApComment;
import com.usian.model.comment.pojos.ApCommentLike;
import com.usian.model.comment.vo.ApCommentVo;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName: CommentServiceImpl
 * Author:   WGG
 * Date:    2022-12-2022/12/6-11:23
 * Version: 1.0
 * Description:
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private ApUserClient apUserClient;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public ResponseResult saveComment(CommentSaveDto dto) {

        //校验参数
        if (dto.getArticleId()==null && dto.getContent()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();

        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }

        //根据查询用户信息
        ApUser apUser = apUserClient.findById(user.getId().longValue());

        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //封装数据
        ApComment apComment = new ApComment();
        apComment.setAuthorId(apUser.getId());
        apComment.setAuthorName(apUser.getName());
        apComment.setContent(dto.getContent());
        apComment.setCreatedTime(new Date());
        apComment.setUpdatedTime(new Date());
        apComment.setEntryId(dto.getArticleId());
        apComment.setImage(apUser.getImage());
        apComment.setLikes(0);
        apComment.setReply(0);
        apComment.setType(0);
        apComment.setFlag(0);

        mongoTemplate.insert(apComment);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult commentLike(CommentLikeDto dto) {

        //校验参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();

        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }

        //根据用户id查询发表评论
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);

        if (apComment==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //根据用户id查询点赞信息
        List<ApCommentLike> apCommentLikes = mongoTemplate.find(Query.query(Criteria.where("authorId").is(user.getId()).and("commentId").is(apComment.getId())), ApCommentLike.class);

        ApCommentLike apCommentLike = new ApCommentLike();

        if (apCommentLikes.size()!=0){
            apCommentLike = apCommentLikes.get(0);
        }

        //判断
        if (dto.getOperation()==0){


            //修改评论表中的数据
            Query query = Query.query(Criteria.where("_id").is(apComment.getId()));
            Update update = new Update().set("likes", apComment.getLikes()==null?1:apComment.getLikes()+1);
            mongoTemplate.updateMulti(query,update,ApComment.class);

            if (apCommentLike.getId()==null){

                //添加点赞信息
                apCommentLike.setAuthorId(user.getId());
                apCommentLike.setCommentId(dto.getCommentId());
                apCommentLike.setOperation(dto.getOperation());
                apCommentLike.setCreatedTime(new Date());
                mongoTemplate.insert(apCommentLike);

            }else {

                //修改点赞信息
                Query query1 = Query.query(Criteria.where("_id").is(apCommentLike.getId()));
                Update update1 = new Update().set("operation", dto.getOperation());
                mongoTemplate.updateMulti(query1,update1,ApCommentLike.class);

            }


        }else {


            //修改评论表中的数据
            Query query = Query.query(Criteria.where("_id").is(apComment.getId()));
            Update update = new Update().set("likes", apComment.getLikes()==0?null:apComment.getLikes()-1);
            mongoTemplate.updateMulti(query,update,ApComment.class);

            //修改点赞信息
            Query query2 = Query.query(Criteria.where("_id").is(apCommentLike.getId()));
            Update update2 = new Update().set("operation", dto.getOperation());
            mongoTemplate.updateMulti(query2,update2,ApCommentLike.class);

        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult findApCommentAndOperation(CommentGetDto dto) {

        //判断用户登录状态
        ApUser user = AppThreadLocalUtils.getUser();

        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }

        //创建返回值集合
        ArrayList<ApCommentVo> apCommentVos = new ArrayList<>();

        ApCommentVo apCommentVo = new ApCommentVo();

        //拿到所有评论数据
        List<ApComment> apComments = mongoTemplate.find(Query.query(Criteria.where("authorId").is(user.getId()).and("entryId").is(dto.getArticleId())),ApComment.class);

        //拿到每条数据
        for (ApComment apComment : apComments) {

            //根据每条数据的id和用户id查询当前评论的点赞状态
            List<ApCommentLike> apCommentLikes = mongoTemplate.find(Query.query(Criteria.where("authorId").is(user.getId()).and("commentId").is(apComment.getId())), ApCommentLike.class);

            if (apCommentLikes!=null){
                apCommentVo.setOperation(apCommentLikes.get(0).getOperation());
            }

            //封装数据
            apCommentVo.setFlag(apComment.getFlag());
            apCommentVo.setUpdatedTime(apComment.getUpdatedTime());
            apCommentVo.setCreatedTime(apComment.getCreatedTime());
            apCommentVo.setContent(apComment.getContent());
            apCommentVo.setAddress(apComment.getAddress());
            apCommentVo.setAuthorName(apComment.getAuthorName());
            apCommentVo.setAuthorId(apComment.getAuthorId());
            apCommentVo.setLikes(apComment.getLikes());
            apCommentVo.setEntryId(apComment.getEntryId());
            apCommentVo.setImage(apComment.getImage());
            apCommentVo.setReply(apComment.getReply());
            apCommentVo.setType(apComment.getType());
            apCommentVo.setChannelId(apComment.getChannelId());
            apCommentVo.setId(apComment.getId());
            apCommentVo.setLatitude(apComment.getLatitude());
            apCommentVo.setLongitude(apComment.getLongitude());
            apCommentVo.setOrd(apComment.getOrd());

            apCommentVos.add(apCommentVo);

        }

        //响应结果
        return ResponseResult.okResult(apCommentVos);
    }

}
